package com.my.study.redisson.register.config;

import com.my.study.redisson.register.DefaultRegisterContext;
import com.my.study.redisson.register.RegisterContext;
import com.my.study.redisson.register.listener.CustomMessageListener;
import com.my.study.redisson.register.properties.RegisterProperties;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: yidujun
 * @create: 2021/11/23 11:05
 */
@Configuration
@EnableConfigurationProperties(value = {RegisterProperties.class})
public class RegisterConfigure {

    private RegisterProperties registerProperties;

    public RegisterConfigure(RegisterProperties registerProperties) {
        this.registerProperties = registerProperties;
    }

    @Bean
    public CustomMessageListener customMessageListener() {
        return new CustomMessageListener();
    }

    @Bean
    @ConditionalOnBean(value = CustomMessageListener.class)
    public RegisterContext registerContext(RedissonClient redissonClient) {
        DefaultRegisterContext registerContext = new DefaultRegisterContext();
        registerContext.setRegisterProperties(registerProperties);
        registerContext.setRedissonClient(redissonClient);

        registerContext.init();
        return registerContext;
    }

}
